<template>
  <div class="product-detail">
    <div v-if="product" class="product-container">
      <img :src="product.image" alt="商品图片" class="product-image">
      <div class="product-info">
        <h2>{{ product.name }}</h2>
        <p>商品类型: {{ getProductTypeName(product.productType) }}</p>
        <p>{{ product.description }}</p>
        <p class="price">¥{{ product.price }}</p>
        <button 
          @click="addToCart" 
          :disabled="!isLoggedIn"
          :title="!isLoggedIn ? '请先登录' : ''"
        >加入购物车</button>
        <router-link to="/">返回首页</router-link>
      </div>
    </div>
    <div v-else>
      <p>商品不存在</p>
      <router-link to="/">返回首页</router-link>
    </div>
  </div>
</template>

<script>
import { ref, onMounted, computed } from 'vue';
import { useRoute } from 'vue-router';
import { useStore } from 'vuex';
import allProducts from '../products.js';

export default {
  name: 'Product',
  setup() {
    const route = useRoute();
    const store = useStore();
    const product = ref(null);
    const isLoggedIn = ref(localStorage.getItem('isLoggedIn') === 'true');

    const getProductTypeName = computed(() => {
      return (productType) => {
        switch (productType) {
          case 'motorcycle':
            return '摩托车';
          case 'gear':
            return '骑行装备';
          case 'parts':
            return '配件改装';
          case 'maintenance':
            return '维护用品';
          default:
            return '';
        }
      };
    });

    // 根据路由参数获取商品
    onMounted(() => {
      const productId = parseInt(route.params.id);
      product.value = allProducts.find(p => p.id === productId);
    });

    // 添加商品到购物车
    const addToCart = () => {
      if (!isLoggedIn.value) {
        alert('请先登录！');
        return;
      }
      if (!product.value) return;

      // 使用 Vuex action 添加商品到购物车
      store.dispatch('addToCart', product.value);
      alert('商品已添加到购物车！');
    };

    return {
      product,
      addToCart,
      isLoggedIn,
      getProductTypeName
    };
  }
};
</script>

<style scoped>
.product-detail {
  text-align: left;
  max-width: 1200px;
  margin: 0 auto;
  padding: 40px 20px;
  min-height: calc(100vh - 60px);
  background: #f0f8ff;
}

.product-container {
  display: flex;
  gap: 40px;
  margin-top: 20px;
  background: white;
  padding: 30px;
  border-radius: 20px;
  box-shadow: 0 10px 30px rgba(0, 59, 123, 0.1);
  animation: fadeIn 0.5s ease;
}

.product-image {
  width: 500px;
  height: 500px;
  object-fit: contain;
  border-radius: 16px;
  background: white;
  box-shadow: 0 8px 24px rgba(0, 59, 123, 0.15);
  transition: all 0.4s ease;
}

.product-image:hover {
  transform: scale(1.02);
  box-shadow: 0 12px 32px rgba(0, 59, 123, 0.2);
}

.product-info {
  flex: 1;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 25px;
}

.product-info h2 {
  font-size: 2.4em;
  color: #002B5B;
  margin-bottom: 15px;
  font-weight: 700;
  line-height: 1.2;
}

.product-info p {
  color: #4a4a4a;
  line-height: 1.8;
  font-size: 1.05em;
  margin-bottom: 15px;
}

.price {
  font-size: 2.2em;
  color: #003B7B;
  font-weight: 700;
  margin: 25px 0;
  padding: 20px;
  background: #f0f8ff;
  border-radius: 16px;
  border: 1px solid rgba(0, 59, 123, 0.1);
  display: inline-block;
}

button[disabled] {
  background-color: #ccc;
  cursor: not-allowed;
  opacity: 0.7;
  transform: none;
}

button {
  width: 100%;
  height: 50px;
  background: #003B7B;
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 1.15em;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  box-shadow: 0 4px 12px rgba(0, 59, 123, 0.2);
  margin-bottom: 15px;
}

button:not(:disabled):hover {
  background: #002B5B;
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(0, 59, 123, 0.3);
}

a {
  display: block;
  text-align: center;
  padding: 15px;
  background: white;
  color: #003B7B;
  text-decoration: none;
  border: 2px solid #003B7B;
  border-radius: 12px;
  font-weight: 600;
  transition: all 0.3s ease;
  margin-top: 15px;
}

a:hover {
  background: #f0f8ff;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 59, 123, 0.15);
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 768px) {
  .product-container {
    flex-direction: column;
    gap: 30px;
    padding: 20px;
  }

  .product-image {
    width: 100%;
    height: 350px;
  }

  .product-info {
    padding: 0;
  }

  .product-info h2 {
    font-size: 2em;
  }

  .price {
    font-size: 1.8em;
  }

  button, a {
    width: 100%;
  }
}
</style>